package com.example.android.utils

import android.util.Log
import androidx.room.TypeConverter
import com.example.android.models.MessageView
import com.google.gson.Gson
import com.google.gson.JsonPrimitive
import java.util.*

/**
 * @ClassName RoomTypeConverter
 * @Author Ryan Kamanri
 * @Date 2022/6/12
 * @Description
 **/
class RoomTypeConverter {
    @TypeConverter
    fun RevertDate(value : Long) : Date {
        return Date(value)
    }

    @TypeConverter
    fun ConvertDate(date : Date) : Long {
        return date.time
    }

    @TypeConverter
    fun RevertObject(value: String) : Any {
        val jsonObject = Gson().fromJson(value, JsonPrimitive::class.java)
        Log.d(toString(), "Content Json Object : $jsonObject")
        if(jsonObject.isString)
            return jsonObject.asString
        Log.w(toString(), "Not Yet Implemented JsonPrimitive To ByteArray")
        return jsonObject.asJsonPrimitive
    }

    @TypeConverter
    fun ConvertObject(value: Any): String {
        return Gson().toJson(value)
    }

    @TypeConverter
    fun RevertViewType(value: Int) : MessageView.ViewType {
        return when(value) {
            0 -> MessageView.ViewType.DATETIME
            1 -> MessageView.ViewType.SELF_TEXT
            2 -> MessageView.ViewType.SELF_IMAGE
            3 -> MessageView.ViewType.OTHER_TEXT
            else -> MessageView.ViewType.OTHER_IMAGE
        }
    }

    @TypeConverter
    fun ConvertViewType(value: MessageView.ViewType) : Int {
        return when(value) {
            MessageView.ViewType.DATETIME -> 0
            MessageView.ViewType.SELF_TEXT -> 1
            MessageView.ViewType.SELF_IMAGE -> 2
            MessageView.ViewType.OTHER_TEXT -> 3
            else -> 4
        }
    }
}